package com.mysoft.gfyx.clue.controller;

import com.mysoft.framework.api.document.DocumentProvider;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.controller.dto.DeleteByIdRequestDto;
import com.mysoft.gfyx.clue.controller.dto.ImportCustomerPoolRequestDto;
import com.mysoft.gfyx.clue.controller.dto.PubAddCustomersRequestDto;
import com.mysoft.gfyx.clue.controller.dto.PubGetCustomerRequestDto;

import com.mysoft.gfyx.clue.service.CustomerPoolService;
import com.mysoft.gfyx.clue.service.CustomerService;
import com.mysoft.gfyx.clue.service.dto.CustomerDto;
import com.mysoft.gfyx.clue.service.dto.ImportCustomerPoolResponseDto;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@PubService(value = "/customerPub", prefix = RequestPrefix.PUB)
public class CustomerPubController extends Controller {

    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerPoolService customerPoolService;

    @Resource
    private DocumentProvider documentProvider;

    @PubAction(value = "/getCustomerByCode")
    public CustomerDto getCustomerByCode(@RequestBody PubGetCustomerRequestDto req) {
        return customerService.getCustomerWithUnique(req.getCustomerResponsiblePersonGUID(), req.getCreditCode());
    }

    @PubAction(value = "/addCustomers")
    public boolean addCustomers(@RequestBody PubAddCustomersRequestDto req) {
        return customerService.addCustomerAndPool(req);
    }

    @PubAction(value = "/isAdded")
    public Set<String> isAdded(@RequestBody List<String> req) {
        return customerPoolService.isAdded(req);
    }

    @PubAction(value = "/deleteCustomerPool")
    public void deleteCustomerPool(@RequestBody DeleteByIdRequestDto req) {
        customerPoolService.deleteCustomerPool(req);
    }

    @PubAction(value = "/importCustomerPool", method = RequestMethod.POST)
    public ImportCustomerPoolResponseDto importCustomerPool(@RequestBody ImportCustomerPoolRequestDto requestDto) throws IOException {
        // 判断文件是否存在
        String fileUrl = documentProvider.getDownloadUrl(requestDto.getDocumentId());
        ImportCustomerPoolResponseDto importCustomerPoolResponseDto = new ImportCustomerPoolResponseDto();
        // 读取 excel 内容
        importCustomerPoolResponseDto.setErrorCompanyList(customerService.importCustomerPool(fileUrl));
        // 记录操作日志

        return importCustomerPoolResponseDto;
    }
}
